package com.leadingsoft.pwxk.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.leadingsoft.common.dto.TableResultDto;
import com.leadingsoft.pwxk.dto.SearchDto;
import com.leadingsoft.pwxk.enums.EDrainCountType;
import com.leadingsoft.pwxk.excel.CommonHandleExcel;
import com.leadingsoft.pwxk.excel.ExcelUtil;
import com.leadingsoft.pwxk.service.DrainCountService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author: lipan 2018年5月25日
 * @description: ( 污染物排放口统计 )
 * 
 */
@Api(description = "污染物排放口统计")
@RestController
@RequestMapping("api/drCount")
public class DrainCountController {

	@Autowired
	private DrainCountService drainCountService;

	@ApiOperation("大气主要排放口合计 - 统计图")
	@GetMapping("airCountOne")
	Map<String, Float> countOne(SearchDto searchDto) {
		return drainCountService.airCountOne(searchDto);
	}

	@ApiOperation("大气主要排放口合计 - 统计表")
	@GetMapping("airCountOneTable")
	TableResultDto<Map<String, Object>> countOneTable(SearchDto searchDto,
			@RequestParam(value = "draw", required = false) Long draw) {
		return drainCountService.airCountOneTable(searchDto, draw);
	}

	@ApiOperation("大气一般排放口合计 - 统计图")
	@GetMapping("airCountTwo")
	Map<String, Float> countTwo(SearchDto searchDto) {
		return drainCountService.airCountTwo(searchDto);
	}

	@ApiOperation("大气一般排放口合计 - 统计表")
	@GetMapping("airCountTwoTable")
	TableResultDto<Map<String, Object>> countTwoTable(SearchDto searchDto,
			@RequestParam(value = "draw", required = false) Long draw) {
		return drainCountService.airCountOneTable(searchDto, draw);
	}

	@ApiOperation("大气排放口合计 - 统计图")
	@GetMapping("airCountThr")
	Map<String, Float> countThr(SearchDto searchDto) {
		return drainCountService.airCountThr(searchDto);
	}

	@ApiOperation("大气排放口合计 - 统计表")
	@GetMapping("airCountThrTable")
	TableResultDto<Map<String, Object>> countThrTable(SearchDto searchDto,
			@RequestParam(value = "draw", required = false) Long draw) {
		return drainCountService.airCountOneTable(searchDto, draw);
	}

	@ApiOperation("水主要排放口合计 - 统计图")
	@GetMapping("waterCountOne")
	Map<String, Float> waterCountOne(SearchDto searchDto) {
		return drainCountService.waterCountOne(searchDto);
	}

	@ApiOperation("水主要排放口合计 - 统计表")
	@GetMapping("waterCountOneTable")
	TableResultDto<Map<String, Object>> waterCountOneTable(SearchDto searchDto,
			@RequestParam(value = "draw", required = false) Long draw) {
		return drainCountService.waterCountOneTable(searchDto, draw);
	}

	@ApiOperation("水一般排放口合计 - 统计图")
	@GetMapping("waterCountTwo")
	Map<String, Float> waterCountTwo(SearchDto searchDto) {
		return drainCountService.waterCountTwo(searchDto);
	}

	@ApiOperation("水一般排放口合计 - 统计表")
	@GetMapping("waterCountTwoTable")
	TableResultDto<Map<String, Object>> waterCountTwoTable(SearchDto searchDto,
			@RequestParam(value = "draw", required = false) Long draw) {
		return drainCountService.waterCountTwoTable(searchDto, draw);
	}

	@ApiOperation("水车间或设施排放口合计 - 图表 ")
	@GetMapping("waterCountThr")
	Map<String, Float> waterCountThr(SearchDto searchDto) {
		return drainCountService.waterCounThr(searchDto);
	}

	@ApiOperation("水车间或设施排放口合计 - 统计表")
	@GetMapping("waterCountThrTable")
	TableResultDto<Map<String, Object>> waterCountThrTable(SearchDto searchDto,
			@RequestParam(value = "draw", required = false) Long draw) {
		return drainCountService.waterCountThrTable(searchDto, draw);
	}

	@ApiOperation("水排放口合计 - 统计图")
	@GetMapping("waterCountFour")
	Map<String, Float> waterCountFour(SearchDto searchDto) {
		return drainCountService.waterCountFour(searchDto);
	}

	@ApiOperation("水排放口合计 - 统计表")
	@GetMapping("waterCountFourTable")
	TableResultDto<Map<String, Object>> waterCountFourTable(SearchDto searchDto,
			@RequestParam(value = "draw", required = false) Long draw) {
		return drainCountService.waterCountFourTable(searchDto, draw);
	}

	@ApiOperation("污染物排放口统计 - 通用导出 excel")
	@RequestMapping(value = "export", method = RequestMethod.GET)
	public void listExcel(HttpServletResponse response, SearchDto searchDto,
			@RequestParam(value = "eGroup", defaultValue = "airCount") EDrainCountType eGroup,
			@RequestParam(value = "etype", defaultValue = "airCountOneTable") EDrainCountType etype,
			@RequestParam(value = "title", required = false) String title) {
		title = etype.getName();
		List<Map<String, Object>> datas = getData(searchDto, etype);
		List<String> titleFirst = new ArrayList<>();
		List<String> propertyList = Arrays.asList(new String[] { "no", "name_region", "entername", "tradesname" });
		List<String> titleList = Arrays.asList(new String[] { "序号", "地区", "单位名称", "行业类别" });
		propertyList = new ArrayList<String>(propertyList);
		titleList = new ArrayList<String>(titleList);
		insertKey(eGroup, propertyList, titleList);
		for (int i = 0; i < propertyList.size(); i++) {
			titleFirst.add(title);
		}
		List<String[]> heads = new ArrayList<>();
		heads.add(titleFirst.toArray(new String[] {}));
		heads.add(titleList.toArray(new String[] {}));
		ExcelUtil.createWorkBook(title, heads, propertyList.toArray(new String[] {}), datas, response,
				new CommonHandleExcel(propertyList.size(), datas.size()), null);
	}

	private void insertKey(EDrainCountType eGroup, List<String> propertyList, List<String> titleList) {
		if (eGroup != EDrainCountType.airCount && eGroup != EDrainCountType.waterCount) {
			throw new RuntimeException("eGroup 支持值为： airCount、waterCount");
		}

		if (eGroup == EDrainCountType.airCount) {
			propertyList.addAll(DrainCountService.AIRCOUNTKEYS);
			titleList.addAll(DrainCountService.AIRCOUNTKEYS);
		}
		if (eGroup == EDrainCountType.waterCount) {
			propertyList.addAll(DrainCountService.WATERCOUNTKEYS);
			titleList.addAll(DrainCountService.WATERCOUNTKEYS);
		}
	}

	private List<Map<String, Object>> getData(SearchDto searchDto, EDrainCountType etype) {
		List<Map<String, Object>> datas = null;
		if (etype == EDrainCountType.airCountTwoTable) {
			datas = drainCountService.airCountTwoTable(searchDto, null).getData();
		} else if (etype == EDrainCountType.airCountThrTable) {
			datas = drainCountService.airCountThrTable(searchDto, null).getData();
		} else if (etype == EDrainCountType.waterCountOneTable) {
			datas = drainCountService.waterCountOneTable(searchDto, null).getData();
		} else if (etype == EDrainCountType.waterCountTwoTable) {
			datas = drainCountService.waterCountTwoTable(searchDto, null).getData();
		} else if (etype == EDrainCountType.waterCountThrTable) {
			datas = drainCountService.waterCountThrTable(searchDto, null).getData();
		} else if (etype == EDrainCountType.waterCountFourTable) {
			datas = drainCountService.waterCountFourTable(searchDto, null).getData();
		}
		if (datas == null) {
			datas = drainCountService.airCountOneTable(searchDto, null).getData();
		}
		datas.forEach(map -> {
			int i = 1;
			map.put("no", i++);
		});
		return datas;
	}

}
